<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="楼盘名称" prop="housingName">
        <el-input
          v-model="queryParams.housingName"
          placeholder="请输入楼盘名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼盘地址" prop="housingAddress">
        <el-input
          v-model="queryParams.housingAddress"
          placeholder="请输入楼盘地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house:housing:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['house:housing:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house:housing:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['house:housing:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="housingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="楼盘ID" align="center" prop="id" />
      <el-table-column label="楼盘名称" align="center" prop="housingName" />
      <el-table-column label="楼盘地址" align="center" prop="housingAddress" />
      <el-table-column label="省" align="center" prop="sheng" />
      <el-table-column label="市" align="center" prop="shi" />
      <el-table-column label="区(县) id" align="center" prop="qu" />
      <el-table-column label="商圈" align="center" prop="businessId" />
      <el-table-column label="地铁" align="center" prop="metroId" />
      <el-table-column label="建筑面积(平方米)" align="center" prop="coveredArea" />
      <el-table-column label="建筑类型" align="center" prop="buildingId" />
      <el-table-column label="社区类型" align="center" prop="community" />
      <el-table-column label="物业类型" align="center" prop="propertyId" />
      <el-table-column label="楼盘特色" align="center" prop="typeId" />
      <el-table-column label="容积率(平方米)" align="center" prop="plotRatio" />
      <el-table-column label="得房率(%)" align="center" prop="gainRatio" />
      <el-table-column label="绿化率" align="center" prop="greeningRate" />
      <el-table-column label="楼栋数(栋)" align="center" prop="buildingNum" />
      <el-table-column label="开发商" align="center" prop="developers" />
      <el-table-column label="物业管理公司" align="center" prop="company" />
      <el-table-column label="楼盘简介" align="center" prop="introduction" />
      <el-table-column label="周边配套" align="center" prop="rimId" />
      <el-table-column label="交通情况" align="center" prop="trafficId" />

      <el-table-column
        prop="floorPlan"
        label="户型图">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="'http://localhost:8102/'+scope.row.floorPlan"></el-image>
        </template>
      </el-table-column>

      <el-table-column
        prop="exteriorPlan"
        label="外景图">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="'http://localhost:8102/'+scope.row.exteriorPlan"></el-image>
        </template>
      </el-table-column>

      <el-table-column
        prop="buildPlan"
        label="施工图">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="'http://localhost:8102/'+scope.row.buildPlan"></el-image>
        </template>
      </el-table-column>

      <el-table-column
        prop="resultPlan"
        label="效果图">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="'http://localhost:8102/'+scope.row.resultPlan"></el-image>
        </template>
      </el-table-column>

      <el-table-column
        prop="homePlan"
        label="首页图">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="'http://localhost:8102/'+scope.row.homePlan"></el-image>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['house:housing:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['house:housing:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改楼盘对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="楼盘名称" prop="housingName">
          <el-input v-model="form.housingName" placeholder="请输入楼盘名称" />
        </el-form-item>
        <el-form-item label="楼盘地址" prop="housingAddress">
          <el-input v-model="form.housingAddress" placeholder="请输入楼盘地址" />
        </el-form-item>
        <el-form-item label="省id" prop="regionId1">
          <el-input v-model="form.regionId1" placeholder="请输入省id" />
        </el-form-item>
        <el-form-item label="市id" prop="regionId2">
          <el-input v-model="form.regionId2" placeholder="请输入市id" />
        </el-form-item>
        <el-form-item label="区(县) id" prop="regionId3">
          <el-input v-model="form.regionId3" placeholder="请输入区(县) id" />
        </el-form-item>
        <el-form-item label="商圈 可以存在多个" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商圈 可以存在多个" />
        </el-form-item>
        <el-form-item label="地铁 可以存在多个" prop="metroId">
          <el-input v-model="form.metroId" placeholder="请输入地铁 可以存在多个" />
        </el-form-item>
        <el-form-item label="建筑面积  (平方米)" prop="coveredArea">
          <el-input v-model="form.coveredArea" placeholder="请输入建筑面积  (平方米)" />
        </el-form-item>
        <el-form-item label="建筑类型id" prop="buildingId">
          <el-input v-model="form.buildingId" placeholder="请输入建筑类型id" />
        </el-form-item>
        <el-form-item label="社区类型id" prop="community">
          <el-input v-model="form.community" placeholder="请输入社区类型id" />
        </el-form-item>
        <el-form-item label="物业类型id" prop="propertyId">
          <el-input v-model="form.propertyId" placeholder="请输入物业类型id" />
        </el-form-item>
        <el-form-item label="楼盘特色 id" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入楼盘特色 id" />
        </el-form-item>
        <el-form-item label="容积率 (平方米)" prop="plotRatio">
          <el-input v-model="form.plotRatio" placeholder="请输入容积率 (平方米)" />
        </el-form-item>
        <el-form-item label="得房率" prop="gainRatio">
          <el-input v-model="form.gainRatio" placeholder="请输入得房率" />
        </el-form-item>
        <el-form-item label="绿化率" prop="greeningRate">
          <el-input v-model="form.greeningRate" placeholder="请输入绿化率" />
        </el-form-item>
        <el-form-item label="楼栋数  (栋)" prop="buildingNum">
          <el-input v-model="form.buildingNum" placeholder="请输入楼栋数  (栋)" />
        </el-form-item>
        <el-form-item label="开发商" prop="developers">
          <el-input v-model="form.developers" placeholder="请输入开发商" />
        </el-form-item>
        <el-form-item label="物业管理公司" prop="company">
          <el-input v-model="form.company" placeholder="请输入物业管理公司" />
        </el-form-item>
        <el-form-item label="楼盘简介" prop="introduction">
          <el-input v-model="form.introduction" placeholder="请输入楼盘简介" />
        </el-form-item>
        <el-form-item label="周边配套" prop="rimId">
          <el-input v-model="form.rimId" placeholder="请输入周边配套" />
        </el-form-item>
        <el-form-item label="交通情况" prop="trafficId">
          <el-input v-model="form.trafficId" placeholder="请输入交通情况" />
        </el-form-item>
        <el-form-item label="户型图" prop="floorPlan">
          <el-input v-model="form.floorPlan" placeholder="请输入户型图" />
        </el-form-item>
        <el-form-item label="外景图" prop="exteriorPlan">
          <el-input v-model="form.exteriorPlan" placeholder="请输入外景图" />
        </el-form-item>
        <el-form-item label="施工图" prop="buildPlan">
          <el-input v-model="form.buildPlan" placeholder="请输入施工图" />
        </el-form-item>
        <el-form-item label="效果图" prop="resultPlan">
          <el-input v-model="form.resultPlan" placeholder="请输入效果图" />
        </el-form-item>
        <el-form-item label="首页图" prop="homePlan">
          <el-input v-model="form.homePlan" placeholder="请输入首页图" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHousing, getHousing, delHousing, addHousing, updateHousing } from "@/api/house/housing";

export default {
  name: "Housing",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 楼盘表格数据
      housingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        housingName: null,
        housingAddress: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询楼盘列表 */
    getList() {
      this.loading = true;
      listHousing(this.queryParams).then(response => {
        this.housingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        housingName: null,
        housingAddress: null,
        regionId1: null,
        regionId2: null,
        regionId3: null,
        businessId: null,
        metroId: null,
        coveredArea: null,
        buildingId: null,
        community: null,
        propertyId: null,
        typeId: null,
        plotRatio: null,
        gainRatio: null,
        greeningRate: null,
        buildingNum: null,
        developers: null,
        company: null,
        introduction: null,
        rimId: null,
        trafficId: null,
        floorPlan: null,
        exteriorPlan: null,
        buildPlan: null,
        resultPlan: null,
        homePlan: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加楼盘";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHousing(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改楼盘";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHousing(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHousing(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除楼盘编号为"' + ids + '"的数据项？').then(function() {
        return delHousing(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house/housing/export', {
        ...this.queryParams
      }, `housing_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
